package com.tab.project;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class TabsActivity extends Activity implements OnClickListener, OnItemSelectedListener {
    /** Called when the activity is first created. */
	
	//Personal Info Vars
	Spinner course;
	EditText nameedit, addressedit, result;
	Button enter;
	TimePicker bday;
	
	//Calc Vars
	int buttonid[] = {R.id.button0, R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6,R.id.button7, R.id.button8, R.id.button9};
	Button[] number = new Button[10];
	EditText IO;
	Button decimal, sign, equals, plus, minus, divide, multiply, clear, factor, exponent;
	String text="";
	double term, answer;
	String prevop;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Setup Tabs
        TabHost thost = (TabHost) findViewById(R.id.tabhost);
        thost.setup();
        
        TabHost.TabSpec spec1 = thost.newTabSpec("tab1");
        spec1.setContent(R.id.tab1);
        spec1.setIndicator("Tab #1");
        thost.addTab(spec1);
        
        TabHost.TabSpec spec2 = thost.newTabSpec("tab2");
        spec2.setContent(R.id.tab2);
        spec2.setIndicator("Tab #2");
        thost.addTab(spec2);
        
        TabHost.TabSpec spec3 = thost.newTabSpec("tab3");
        spec3.setContent(R.id.tab3);
        spec3.setIndicator("Tab #3");
        thost.addTab(spec3);
        
        //Personal Info Code
        //Setup text
        TextView nametext = (TextView) findViewById(R.id.textname);
        nametext.setText("Name");
        TextView addresstext = (TextView) findViewById(R.id.textaddress);
        addresstext.setText("Address");
        TextView bdaytext = (TextView) findViewById(R.id.textbday);
        bdaytext.setText("Birth Date");
        //Setup text boxes
        nameedit = (EditText) findViewById(R.id.editTextname);
        nameedit.setText("");
        addressedit = (EditText) findViewById(R.id.editTextaddress);
        addressedit.setText("");
        result = (EditText) findViewById(R.id.editTextresult);
        //Setup interactive things
        enter = (Button) findViewById(R.id.buttonenter);
        enter.setText("Enter");
        enter.setOnClickListener(this);
        
        course = (Spinner) findViewById(R.id.spinnercourse);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.array1,android.R.layout.simple_spinner_item);
        course.setAdapter(adapter);
        //SEE THIS
        course.setOnItemSelectedListener(this);
        
        bday = (TimePicker) findViewById(R.id.timePicker);
        
        //Calc Code
      //Setup # buttons

      		for(int i = 0; i < 10; i++)
      		{
      			number[i] = (Button) findViewById(buttonid[i]);
      			number[i].setText(Integer.toString(i));
      			number[i].setOnClickListener(this);
      		}
      		//Setup non-# buttons
      		IO = (EditText) findViewById(R.id.IO);
      		decimal = (Button) findViewById(R.id.buttondecimal);
      		sign = (Button) findViewById(R.id.buttonsign);
      		equals = (Button) findViewById(R.id.buttonequals);
      		plus = (Button) findViewById(R.id.buttonplus);
      		minus = (Button) findViewById(R.id.buttonminus);
      		divide = (Button) findViewById(R.id.buttondivide);
      		multiply = (Button) findViewById(R.id.buttonmultiply);
      		clear = (Button) findViewById(R.id.buttonclear);
      		factor = (Button) findViewById(R.id.buttonfactor);
      		exponent = (Button) findViewById(R.id.buttonexponent);

      		exponent.setText("^");
      		exponent.setOnClickListener(this);

      		factor.setText("!");
      		factor.setOnClickListener(this);

      		decimal.setText(".");
      		decimal.setOnClickListener(this);

      		sign.setText("+/-");
      		sign.setOnClickListener(this);

      		equals.setText("=");
      		equals.setOnClickListener(this);

      		plus.setText("+");
      		plus.setOnClickListener(this);

      		minus.setText("-");
      		minus.setOnClickListener(this);

      		divide.setText("/");
      		divide.setOnClickListener(this);

      		multiply.setText("*");
      		multiply.setOnClickListener(this);

      		clear.setText("Clear");
      		clear.setOnClickListener(this);

      		IO.setOnClickListener(this);

      		//Set Setting to Clear
      		text = "";
      		IO.setText(text);
      		term = 0;
      		prevop = "";
      		answer = 0;
      		
      		//Ball Code
      		
      		
      	}
      	//Button Functionality
      	public void onClick(View v) {
      		//Personal Info Enter
    		if(v.equals(enter))
    		{
    			if(nameedit.getText().toString().equals("") || addressedit.getText().toString().equals("") || course.getSelectedItem().toString().equals("Course"))
    			{
    				Toast.makeText(this, "Please complete all fields.", Toast.LENGTH_SHORT).show();
    			}
    			else
    			{
    				result.setText(nameedit.getText().toString() + "\n" + addressedit.getText().toString() + "\n" + bday.getCurrentHour() + ":" + bday.getCurrentMinute() + "\n" + course.getSelectedItem().toString());
    				nameedit.setText("");
    				addressedit.setText("");
    				course.setSelection(0);
    			}
    		}
      		//Clear Settings
      		if(v.equals(clear))
      		{
      			text = "";
      			term = 0;
      			IO.setText(text);
      			prevop = "";
      			answer = 0;
      		}
      		//Typing #s
      		for(int i = 0; i < 10; i++)
      		{
      			if(v.equals(number[i]))
      			{
      				text += ((Button) v).getText();
      				if(prevop == "")
      				{
      					IO.setText(text);
      				}
      				else
      				{
      					IO.setText(term + "\n" + text);
      				}
      			}
      		}
      		//Decimals
      		if(v.equals(decimal))
      		{
      			if(text.contains(".") == false)
      			{
      				text += ".";
      				if(prevop == "")
      				{
      					IO.setText(text);
      				}
      				else
      				{
      					IO.setText(term + "\n" + text);
      				}
      			}
      		}
      		//Changing sign
      		if(v.equals(sign))
      		{
      			if(!text.startsWith("-"))
      			{
      				text = "-" + text;
      			}
      			else
      			{
      				text = text.substring(1);
      			}
      			IO.setText(text);
      		}
      		//Operations
      		if(v.equals(multiply))
      		{
      			if(text == "")
      			{
      				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
      			}
      			else
      			{
      				if(prevop != "")
      				{
      					answer = compute(answer, Double.parseDouble(text));
      				}
      				if(prevop == "")
      				{
      					answer = Double.parseDouble(text);
      				}
      				term = Double.parseDouble(text);
      				prevop = "multiplication";
      				text = "";
      				IO.setText(Double.toString(term) + "\n" + text);
      			}
      		}
      		if(v.equals(divide))
      		{
      			if(text == "")
      			{
      				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
      			}
      			else
      			{
      				if(prevop != "")
      				{
      					answer = compute(answer, Double.parseDouble(text));
      				}
      				if(prevop == "")
      				{
      					answer = Double.parseDouble(text);
      				}
      				term = Double.parseDouble(text);
      				prevop = "division";
      				text = "";
      				IO.setText(Double.toString(term) + "\n" + text);
      			}
      		}
      		if(v.equals(plus))
      		{
      			if(text == "")
      			{
      				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
      			}
      			else
      			{
      				if(prevop != "")
      				{
      					answer = compute(answer, Double.parseDouble(text));
      				}
      				if(prevop == "")
      				{
      					answer = Double.parseDouble(text);
      				}
      				term = Double.parseDouble(text);
      				prevop = "addition";
      				text = "";
      				IO.setText(Double.toString(term) + "\n" + text);
      			}
      		}
      		if(v.equals(minus))
      		{
      			if(text == "")
      			{
      				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
      			}
      			else
      			{
      				if(prevop != "")
      				{
      					answer = compute(answer, Double.parseDouble(text));
      				}
      				if(prevop == "")
      				{
      					answer = Double.parseDouble(text);
      				}
      				term = Double.parseDouble(text);
      				prevop = "subtraction";
      				text = "";
      				IO.setText(Double.toString(term) + "\n" + text);
      			}
      		}
      		if(v.equals(factor))
      		{
      			if(text == "")
      			{
      				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
      			}
      			else
      			{
      				if(text.contains("."))
      				{
      					Toast.makeText(this, "Can only perform with integers.", Toast.LENGTH_SHORT).show();
      				}
      				else
      				{
      					double temp = factorial(Double.parseDouble(text));
      					text = Double.toString(temp);
      				}
      				if(prevop == "")
      				{
      					IO.setText(text);
      				}
      				else
      				{
      					IO.setText(term + "\n" + text);
      				}
      			}
      		}
      		if(v.equals(exponent))
      		{
      			if(text == "")
      			{
      				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
      			}
      			else
      			{
      				if(prevop != "")
      				{
      					answer = compute(answer, Double.parseDouble(text));
      				}
      				if(prevop == "")
      				{
      					answer = Double.parseDouble(text);
      				}
      				term = Double.parseDouble(text);
      				prevop = "exponential";
      				text = "";
      				IO.setText(Double.toString(term) + "\n" + text);
      			}
      		}
      		//Equals
      		if(v.equals(equals))
      		{
      			if(text == "")
      			{
      				Toast.makeText(this, "Enter next term.", Toast.LENGTH_SHORT).show();
      			}
      			else
      			{
      				answer = compute(term, Double.parseDouble(text));
      				term = 0;
      				prevop = "";
      				text = Double.toString(answer);
      				IO.setText(text);
      			}
      		}
      	}
      	//The actual math
      	public double compute(double a, double b){
      		double c = 0;
      		if(prevop == "multiplication")
      		{
      			c = a * b;
      		}
      		else if(prevop == "addition")
      		{
      			c = a + b;
      		}
      		else if(prevop == "subtraction")
      		{
      			c = a - b;
      		}
      		else if(prevop == "division")
      		{
      			c = a / b;
      		}
      		else if(prevop == "exponential")
      		{
      			c = 1;
      			for(double i = b; i > 0; i--)
      			{
      				c *= a;
      			}
      		}
      		return c;
      	}
      	public double factorial(double a){
      		for(int i = (int) a - 1; i > 0; i--)
      		{
      			a *= i;
      		}
      		return a;
      	}
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
		}
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
      	
      	
      	
    }